package test;

import gui.settings.ConfigFile;

import java.io.File;
import java.util.Collections;
import java.util.LinkedList;

import javax.swing.filechooser.FileSystemView;

import lbd.Lbd;
import lbd.TermFreq;
import lbd.TermFreqComparator;

import org.apache.lucene.index.IndexReader;

/**
 * The Class Main.
 */
public class Main {
	
	/**
	 * The main method.
	 * 
	 * @param args The arguments given by command line
	 */
	public static void main(String[] args) {
		
		LinkedList<TermFreq> results = null;
		
		//Indecises all the documents in the "documents/" directory
		Lbd.indeces( FileSystemView.getFileSystemView().getFiles( new File(Lbd.DOCS_DIRECTORY), true ) , ".pdf"); 
		
		try {
			
			//Checks if the index exists
			if( IndexReader.indexExists("index") )
			{
			
				IndexReader reader = IndexReader.open("index");
				
				//Runs the follow iteration for every document in the list
				for( int d = 0 ; d < reader.numDocs() ; d++ )
				{
					
					//Gets the terms list ordered by frequency
					results = Lbd.getTermsOrderByFreq( "index", d );
					//Filters the terms list with the given minimum occurrences
					results = Lbd.filterTermsList( results, ConfigFile.getInstance().getTERM_NUM(), ConfigFile.getInstance().getTERM_MIN_FREQ() );
					//Adds term's synonyms to the terms list
					if(ConfigFile.getInstance().isADD_SYNONYMS()) Lbd.addSynonymsToTermsList( results, d );
					
					TermFreqComparator tfc = new TermFreqComparator();
					//Orders the list by the frequency
					Collections.sort( results, tfc );
					
					Lbd.addAdjacentTermsToTermsList( results, "index", d );
					
					Lbd.printAllTerms( results, d, true, true );
					
					Lbd.createChart("Pie Chart 3D", Lbd.getDocumentFileName("index", d), results);
					
					results = null;
				}
					
			}
			
		} catch (Exception e) { e.printStackTrace(); }
		
		
	}
	
}


